package com.txd.mofa.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseframework.util.Preferences;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.MainActivity;
import com.txd.mofa.R;
import com.txd.mofa.net.NetStateReceiver;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.view.XieyiDialog;

import java.util.Map;

import static com.txd.mofa.http.HttpUtils.advertOfLogin;


/**
 * 创建时间： 2019/2/25 10:02
 * 功能描述： 启动页
 * 联系方式：1037438704@qq.com
 *
 * @author dell-pc
 */
public class StartAty extends AppCompatActivity {
    private long DELAY_TIME = 0L;
    private int anInt;
    private View view;
    private String token;
    private Map<String, String> data;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!this.isTaskRoot()) {
            //判断该Activity是不是任务空间的源Activity，“非”也就是说是被系统重新实例化出来
            //如果你就放在launcher Activity中话，这里可以直接return了
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                //finish()之后该活动会继续执行后面的代码，你可以logCat验证，加return避免可能的exception
                return;
            }
        }

        http();

        view = View.inflate(this, R.layout.aty_start, null);
        setContentView(view);
        token = Preferences.getInstance().getString(this, "mofa", "token");
        anInt = Preferences.getInstance().getInt(this, "user2", "bootpage");
        Preferences.getInstance().set(this, "user2", "bootpage", ++anInt);
//
//        if (Preferences.getInstance().getInt(this,"tongyi","tongyi") != 1){
//
//            XieyiDialog xieyiDialog = new XieyiDialog(this, new XieyiDialog.SignListener() {
//                @Override
//                public void dia() {
                    //渐变展示启动屏
                    AlphaAnimation aa = new AlphaAnimation(0.05f, 1.0f);
                    aa.setDuration(3000);
                    view.startAnimation(aa);
                    aa.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationEnd(Animation arg0) {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    // 第一次进入应用  进入引导页  第二次进入  进入 登录界面   判断是否登录  或者判断数据库中是否有数据  然后直接进入主界面
//                                    if (anInt == 1) {
//                                        startActivity(new Intent(StartAty.this, BootPageAty.class));
                                    if (Preferences.getInstance().getInt(StartAty.this,"tongyi","tongyi") != 1){
                                        Intent intent = new Intent(StartAty.this,XieyiZhengceAty.class);
                                        intent.putExtra("pos",0);
                                        intent.putExtra("first","111");
                                        intent.putExtra("flag","222");
                                        startActivity(intent);

                                        finish();
                                    } else {
//                                        if (anInt >= 1) {
                                        if (NetUtils.isNetworkAvailable(StartAty.this)){
                                            if (data != null) {
                                                Preferences.getInstance().set(StartAty.this, "url", "url", data.get("img_url"));
                                                Preferences.getInstance().set(StartAty.this, "zhi", "zhi", data.get("value"));
                                                Preferences.getInstance().set(StartAty.this, "id", "id", data.get("id"));
                                                Intent intent = new Intent(StartAty.this, QidongyeAty.class);
                                                intent.putExtra("url", data.get("img_path"));
                                                intent.putExtra("type", data.get("type"));
                                                intent.putExtra("zhi", data.get("value"));
                                                startActivity(intent);

                                            }
                                            finish();
                                        }else {
                                            if (token != null && !token.equals("")) {
                                                startActivity(new Intent(StartAty.this, MainActivity.class));
                                            } else {
                                                startActivity(new Intent(StartAty.this, LoginAty.class));//login
                                            }
                                        }

                                    }
                                }
                            }, DELAY_TIME);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                    });
//                }
//            });
//            xieyiDialog.show();
//        }else {//渐变展示启动屏
//            AlphaAnimation aa = new AlphaAnimation(0.05f, 1.0f);
//            aa.setDuration(3000);
//            view.startAnimation(aa);
//            aa.setAnimationListener(new Animation.AnimationListener() {
//                @Override
//                public void onAnimationEnd(Animation arg0) {
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            // 第一次进入应用  进入引导页  第二次进入  进入 登录界面   判断是否登录  或者判断数据库中是否有数据  然后直接进入主界面
//                            if (anInt == 1) {
//                                startActivity(new Intent(StartAty.this, BootPageAty.class));
//                                finish();
//                            } else if (anInt >= 1) {
//                                if (NetUtils.isNetworkAvailable(StartAty.this)){
//                                    if (data != null) {
//                                        Preferences.getInstance().set(StartAty.this, "url", "url", data.get("img_url"));
//                                        Preferences.getInstance().set(StartAty.this, "zhi", "zhi", data.get("value"));
//                                        Preferences.getInstance().set(StartAty.this, "id", "id", data.get("id"));
//                                        Intent intent = new Intent(StartAty.this, QidongyeAty.class);
//                                        intent.putExtra("url", data.get("img_path"));
//                                        intent.putExtra("type", data.get("type"));
//                                        intent.putExtra("zhi", data.get("value"));
//                                        startActivity(intent);
//
//                                    }
//                                    finish();
//                                }else {
//                                    if (token != null && !token.equals("")) {
//                                        startActivity(new Intent(StartAty.this, MainActivity.class));
//                                    } else {
//                                        startActivity(new Intent(StartAty.this, LoginAty.class));//login
//                                    }
//                                }
//
//                            }
//                        }
//                    }, DELAY_TIME);
//                }
//
//                @Override
//                public void onAnimationRepeat(Animation animation) {
//                }
//
//                @Override
//                public void onAnimationStart(Animation animation) {
//                }
//
//            });
//        }



    }


    private void http() {

        if (!NetUtils.isNetworkAvailable(this)) {
            return;
        }

        HttpRequest.POST(StartAty.this, advertOfLogin, new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")) {
                        data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                    }

                } else {
                    Toast.makeText(StartAty.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
